<template>
  <j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk"
    @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="料件类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['materielType', validatorRules.materialType]" :trigger-change="true"
            dictCode="material_type" placeholder="请选择料件类型" />
        </a-form-item>
        <a-form-item label="商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['productCode']" placeholder="请输入商品编码"></a-input>
        </a-form-item>
        <a-form-item label="客户料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientCode', validatorRules.clientCode]" :trigger-change="true" placeholder="请输入客户料号"></a-input>
        </a-form-item>
        <a-form-item label="内部料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup v-decorator="['materielCode', validatorRules.materielCode]" :trigger-change="true" org-fields="id,materiel_code,product_name,gauge,materiel_type,unit"
            dest-fields="materielId,materielCode,materielName,materielGauge,materialType,unit" code="materiel_search"
            @callback="popupCallback" />
        </a-form-item>
        <a-form-item label="料件名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName', validatorRules.materielName]" :trigger-change="true" placeholder="请输入物料名称" disabled></a-input>
        </a-form-item>
        <a-form-item label="料件规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielGauge', validatorRules.materielGauge]" :trigger-change="true" placeholder="请输入物料规格" disabled></a-input>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unit', validatorRules.unit]" :trigger-change="true" placeholder="请输入单位"></a-input>
        </a-form-item>
        <a-form-item label="误差" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['differences']" placeholder="请输入客户料号"></a-input>
        </a-form-item>

        <a-form-item label="伏数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['voltage']" placeholder="请输入客户料号"></a-input>
        </a-form-item>
        <a-form-item label="封装" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['packup']" placeholder="请输入封装"></a-input>
        </a-form-item>
        <a-form-item label="物料阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['materielGrade', validatorRules.produceGrade]" :trigger-change="true" dictCode="produce_grade" placeholder="请选择物料阶别"/>
        </a-form-item>
        <a-form-item label="损耗率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['intangibleRate', validatorRules.intangibleRate]" :trigger-change="true" placeholder="请输入无形损耗率"></a-input>
        </a-form-item>
        <!-- <a-form-item label="点位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['spot']" placeholder="请输入点位"></a-input>
        </a-form-item> -->
        <a-form-item label="正面用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['quantity', validatorRules.quantity]" :trigger-change="true" placeholder="请输入正面用量"></a-input>
        </a-form-item>
        <a-form-item label="反面用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['inverseQuantity']" placeholder="请输入反面用量"></a-input>
        </a-form-item>
        <a-form-item label="位号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['positionNum']" placeholder="请输入位号（料件类型为PCB不用填此项）"></a-input>
        </a-form-item>
        <a-form-item label="半成品类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materialType']" placeholder="是半成品请输入BCP 不是则留空"></a-input>
        </a-form-item>
        <a-form-item label="最小包装数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['packType']" placeholder="请输入最小包装数"></a-input>
        </a-form-item>
        <!-- <a-form-item label="净耗" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unitConsume']" placeholder="请输入净耗"></a-input>
        </a-form-item> -->
        <!-- <a-form-item label="有形损耗率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['wastageRate',validatorRules.wastageRate]" placeholder="请输入损耗率,若无请输入0"></a-input>
        </a-form-item> -->
        <!-- <a-form-item label="反面点位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['inverseSpot']" placeholder="请输入反面点位"></a-input>
        </a-form-item> -->
        <!-- <a-form-item label="正面点位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['frontSpot']" placeholder="请输入正面点位"></a-input>
        </a-form-item>
        <a-form-item label="正面用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['frontQuantity']" placeholder="请输入正面用量"></a-input>
        </a-form-item> -->
        
       <!--
        <a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['produceGrade']" placeholder="请输入客户料号"></a-input>
        </a-form-item> -->
        <a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {
    httpAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import {
    validateDuplicateValue
  } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesChiefdataBomitemModal",
    components: {
      JDictSelectTag,
    },
    props: {
      mainId: {
        type: String,
        required: false,
        default: ''
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        validatorRules: {
          materielCode: {
            rules: [{
              required: true,
              message: '请选择内部料号!'
            }, ]
          },
          unit: {
            rules: [{
              required: true,
              message: '请输入单位!'
            }, ]
          },
          spot: {
            rules: [{
              required: true,
              message: '请输入点位!'
            }, ]
          },
          quantity: {
            rules: [{
              required: true,
              message: '请输入正面用量!'
            }, ]
          },
          wastageRate: {
            rules: [{
              required: true,
              message: '请输入损耗率!'
            }, ]
          },
          materialType: {
            rules: [{
              required: true,
              message: '请输入料件类型!'
            }, ]
          },
          productCode: {
            rules: [{
              required: true,
              message: '请输入商品编码!'
            }, ]
          },
          materielName: {
            rules: [{
              required: true,
              message: '请输入品名!'
            }, ]
          },
          materielGauge: {
            rules: [{
              required: true,
              message: '请输入料件规格!'
            }, ]
          },
          intangibleRate: {
            rules: [{
              required: true,
              message: '请输入损耗率!'
            }, ]
          },
          packType: {
            rules: [{
              required: true,
              message: '请输入组装类型!'
            }, ]
          },
          clientCode: {
            rules: [{
              required: true,
              message: '请输入客户料号!'
            }, ]
          },

        },
        url: {
          add: "/chiefdata/mesChiefdataBom/addMesChiefdataBomitem",
          edit: "/chiefdata/mesChiefdataBom/editMesChiefdataBomitem",
        }

      }
    },
    created() {},
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'createBy', 'createTime', 'updateBy', 'updateTime',
            'sysOrgCode', 'materialType', 'unitConsume', 'intangibleRate', 'materielId', 'productCode',
            'materielCode', 'materielName', 'materielGauge', 'unit', 'positionNum', 'packup', 'packType',
            'spot', 'quantity', 'materielGrade',
            'inverseSpot', 'inverseQuantity', 'frontSpot', 'frontQuantity', 'materielType', 'bomId',
            'wastageRate', 'query3', 'query4', 'query5', 'query6','clientCode','differences','voltage','produceGrade'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['bomId'] = this.mainId
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'createBy', 'createTime', 'updateBy', 'updateTime', 'sysOrgCode',
          'materialType', 'unitConsume', 'intangibleRate', 'materielId', 'productCode', 'materielCode',
          'materielName', 'materielGauge', 'unit', 'positionNum', 'packup', 'packType', 'spot', 'quantity',
          'materielGrade', 'inverseSpot',
          'inverseQuantity', 'frontSpot', 'frontQuantity', 'materielType', 'bomId', 'wastageRate', 'query3',
          'query4', 'query5', 'query6','clientCode','differences','voltage','produceGrade'))
      },


    }
  }
</script>
